package com.example.demo.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**大华视频签名
 * @author xumf
 * @date 2019/9/24 16:24
 */
public class SignVideoUtils {

    public static String videoSign(String realm_value,String randomKey,String user_value,String pass_value) throws NoSuchAlgorithmException {


//        MessageDigest md5 = MessageDigest.getInstance("MD5");

        String passwd_value0 = md5Function(pass_value);
        //2
        String passwd_value1 = md5Function(user_value + passwd_value0);
        //3
        String passwd_temp = md5Function(passwd_value1);
        //4
        String encrypted_passwd = md5Function(user_value + ":" + realm_value + ":" + passwd_temp);
        //5
        String signstr = md5Function(encrypted_passwd + ":" + randomKey);

        return signstr;
    }

    public static String md5Function(String src) {
        try {
            // 加密对象，指定加密方式
            MessageDigest md5 = MessageDigest.getInstance("md5");
            // 准备要加密的数据
            byte[] b = src.getBytes();
            // 加密
            byte[] digest = md5.digest(b);
            // 十六进制的字符
            char[] chars = new char[]{'0', '1', '2', '3', '4', '5',
                    '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
            StringBuffer sb = new StringBuffer();
            // 处理成十六进制的字符串(通常)
            for (byte bb : digest) {
                sb.append(chars[(bb >> 4) & 15]);
                sb.append(chars[bb & 15]);
            }
            // 打印加密后的字符串
//            System.out.println(sb.toString().toLowerCase());
            return sb.toString().toLowerCase();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
                String user_value="system";
        String pass_value="zsxxzx2280191";
        String realm_value = "TheNextService";
        String randomKey = "54b53072540eeeb8f8e9343e71f28176_849b042d-2a09-4bd2-9bd1-239f5937afe3";
//        String user_value = "hsajdj";
//        String pass_value = "admin12345";
        System.out.println(videoSign(realm_value,randomKey,user_value,pass_value));
        // 需要加密的字符串
//        String src = "123456";

    }


}
